#include "groupsessiondetailwidget.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>

#include "debug.h"
#include "widgetkit/basewidget.h"
#include "utils/widgetuitl.hpp"
#include "model/datacenter.h"

GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget* parent)
    : QDialog(parent), curRow(0), curCol(1)
{
    // 1.设置窗口基本属性
    this->setStyleSheet("QWidget {background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(350, 600);
    this->setWindowTitle("群组详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));

    // 2.整个窗口垂直布局
    QVBoxLayout* mainlayout = new QVBoxLayout();
    mainlayout->setContentsMargins(30, 20, 30, 50);
    mainlayout->setSpacing(0);
    mainlayout->setAlignment(Qt::AlignTop);
    this->setLayout(mainlayout);

    // 3.创建成员列表区
    QScrollArea* scrollArea = utils::WidgetUtil::createQScrollArea();
    mainlayout->addWidget(scrollArea);

    QWidget* container = new QWidget();
    scrollArea->setWidget(container);

    // 3-1 存放container内的控件采用网格布局
    containerLayout = new QGridLayout();
    containerLayout->setVerticalSpacing(3);
    containerLayout->setHorizontalSpacing(0);
    containerLayout->setContentsMargins(0, 0, 0, 0);
    containerLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    container->setLayout(containerLayout);

    // 3-2. 添加邀请按钮
    AvatarItem* addBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加");
    addBtn->getAvatarBtn()->setStyleSheet("QPushButton { border: 1px solid rgb(218, 218, 218); }");
    containerLayout->addWidget(addBtn, 0, 0);

    // 4. 群聊名称区域布局管理
    groupNameLayout(mainlayout);

    // 5. 退出群聊按钮
    QPushButton* exitGroupBtn = new QPushButton();
    exitGroupBtn->setText("退出群聊");
    exitGroupBtn->setFixedSize(200, 50);
    exitGroupBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    exitGroupBtn->setStyleSheet("QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; } "
                                "QPushButton:pressed { background-color: rgb(235, 235, 235); }");
    mainlayout->addWidget(exitGroupBtn, 0, Qt::AlignCenter);

////////////// 构造测试数据
#if TEST_UI
    QIcon icon(":/resource/image/girl.jpg");
    for(int i = 0; i < 30; ++i) {
        addMember(new AvatarItem(icon, "超级无敌小美" + QString::number(i)));
    }
#endif

}

void GroupSessionDetailWidget::addMember(AvatarItem* avatarItem)
{
    static const int MAX_COL = 4;
    if(curCol >= MAX_COL) {
        ++curRow;
        curCol = 0;
    }
    containerLayout->addWidget(avatarItem, curRow, curCol);
    ++curCol;
}

void GroupSessionDetailWidget::groupNameLayout(QVBoxLayout* mainLayout)
{
    // 1. 中间区域垂直布局
    QVBoxLayout* midMainlayout = new QVBoxLayout();
    midMainlayout->setSpacing(0);
    midMainlayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addLayout(midMainlayout);

    // 2. "群聊名称" 标签
    QLabel* groupNameKey = new QLabel();
    groupNameKey->setText("群聊名称");
    groupNameKey->setFixedHeight(40);
    groupNameKey->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    groupNameKey->setStyleSheet("QLabel { font-weight: 700; font-size: 17px; }");
    groupNameKey->setAlignment(Qt::AlignBottom);
    midMainlayout->addWidget(groupNameKey);

    // 6.水平布局群聊名称数据行和修改按钮
    QHBoxLayout* groupNameValLayout = new QHBoxLayout();
    groupNameValLayout->setSpacing(0);
    groupNameValLayout->setContentsMargins(0, 0, 0, 15);
    midMainlayout->addLayout(groupNameValLayout);      // 添加到中间区域主布局器

    groupNameVal = new QLabel();
    groupNameVal->setFixedHeight(40);
    groupNameVal->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    groupNameVal->setStyleSheet("QLabel { font-size: 17px; }");
    groupNameValLayout->addWidget(groupNameVal, 0, Qt::AlignTop | Qt::AlignLeft);

    QPushButton* modifyBtn = new QPushButton();
    modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    modifyBtn->setFixedSize(30, 30);
    modifyBtn->setIconSize(QSize(20, 20));
    modifyBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    modifyBtn->setStyleSheet("QPushButton { border: none; } QPushButton:pressed { background-color: rgb(235, 235, 235); }");
    groupNameValLayout->addWidget(modifyBtn);

////////////// 构造测试数据
#if TEST_UI
    groupNameVal->setText("后宫群");
#endif

    // 初始化数据
    initData();
}

void GroupSessionDetailWidget::initData()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::getMemberListDone, this, &GroupSessionDetailWidget::initSessionMembers);
    dataCenter->getMemberListAsync(dataCenter->getCurrentChatSessionId());
}

void GroupSessionDetailWidget::initSessionMembers(const QString& chatSessionId)
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    QList<model::UserInfo>* memberList = dataCenter->getMemberList(chatSessionId);
    if(!memberList) {
        LOG(LL_ERROR, "成员列表没有正确被加载");
        return;
    }

    for(const auto& m : *memberList) {
        AvatarItem* item = new AvatarItem(m.avatar, m.nickname);
        this->addMember(item);
    }

    groupNameVal->setText(dataCenter->getChatSessionInfoBySessionId(dataCenter->getCurrentChatSessionId())->chatSessionName);
}
